<?php 
/**************************************************/
## Class Name - Model_COUNTRY (Contains all the functions related Country modules)
## Created By - Techmodi (22/08/2011)
/**************************************************/
class Model_State extends Database
{	
	function Model_State() {	
		$this->state = STATE;
		$this->country = COUNTRY;	
		$this->City = CITY;		
		$this->Postalcode =POSTALCODE;	
		$this->Database();
	}
	
	// for add state
	function addState($array) {
		$this->InsertData($this->state, $array);
		$insertId = mysql_insert_id();
		return $insertId;
	}
	
	function addPostalcode($array) {
		$this->InsertData($this->Postalcode, $array);
		$insertId = mysql_insert_id();
		return $insertId;
	}
	
	## List all state
	function getAllState($searchindex='', $search_country='',$col1='',$col2='',$col3='', $limit='',$offset='') {
		$fields = array('s.*, c.country_name');	
		$tables = array($this->state." AS s", $this->country." AS c");
		$where= array("s.country_id=c.country_id");
		
		if($search_country != '' && $search_country != 0) {
			$where[] = "s.country_id=".trim($search_country);
		}
		
		if($searchindex != '' && $searchindex!='State') {
			$where[] = "s.state_name LIKE '%".trim($searchindex)."%'";
		}
		if($col1 =='0')
		{
			$order = array("state_name ASC");
		}
		if($col2 =='0')
		{
			$order = array("iso1_code ASC");
		}
		if($col3=='0')
		{
			$order = array("country_name ASC");
		}
		if($col1 =='1')
		{
			$order = array("state_name DESC");
		}
		if($col2 =='1')
		{
			$order = array("iso1_code DESC");
		}
		if($col3=='1')
		{
			$order = array("country_name DESC");
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get state details by stateid
	function getStateDetailsById($stateid) {
		$fields = array();	
		$tables = array($this->state);
		$result1 = $this->SelectData($fields,$tables, $where= array("state_id=".$stateid), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}
	
	## Edit Country by stateid
	function editStateById($array, $stateid){
		$this->UpdateData($this->state,$array,"state_id",$stateid,0);
	}
	
	## Update State status with multiple stateids
	function updateStateStatus($stateid, $status) {		
		$sql = "UPDATE ".$this->state." SET state_status='".$status."' WHERE state_id = (".$stateid.")";
		$result1= $this->ExecuteQuery($sql);	 
		
	}
	
	## Delete country with multiple stateids
	function deleteCountry($stateids, $status) {
		$sql = "DELETE FROM ".$this->state." WHERE state_id IN (".$stateids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete Country cat by id
	function deleteStateById($stateid){
		$this->DeleteData($this->City,"state_id",$stateid);
		$this->DeleteData($this->state,"state_id",$stateid);
	}
	
	/****************************************************/
	## Update country status with multiple stateids
	function updateMultipleStateStatus($stateids, $status) {
		$sql = "UPDATE ".$this->state." SET state_status='".$status."' WHERE state_id IN (".$stateids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete country category with multiple stateids
	function deleteMultipleState($stateids) {
		$sql = "DELETE FROM ".$this->City." WHERE state_id IN (".$stateids.")";		
		$sql2 = "DELETE FROM ".$this->state." WHERE state_id IN (".$stateids.")";
		$result1= $this->ExecuteQuery($sql);	 
		$result1= $this->ExecuteQuery($sql2);	 
	}
	
	## List all active state name
	function getAllStateWithActiveStatus() {
		$fields = array('');	
		$tables = array($this->state);
		$where= array("state_status='1'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("state_name ASC"), $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	## Get country name by country id
	
	function getCountryNameByCountryId($countryid) {
		$fields = array('country_name');	
		$tables = array($this->country);
		$result1 = $this->SelectData($fields,$tables, $where= array("country_id='".$countryid."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	## List all active country name
	function getAllCountryWithActiveStatus() {
		$fields = array('');	
		$tables = array($this->country);
		$where= array("country_status='1'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	## List all active country name
	function getAllstateOnCitypage() {
		$fields = array('');	
		$tables = array($this->state);
		$where= array();
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	## Get country details by countryid
	function getCountryDetailsById($countryid) {
		$fields = array();	
		$tables = array($this->country);
		$result1 = $this->SelectData($fields,$tables, $where= array("country_id='".$countryid."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	##get all the state written by sandeep shirsat 23 aug 2011
	function getAllStateByCountryId($countryid) {
		$fields = array();	
		$tables = array($this->state);
		$where= array("country_id=".$countryid." and state_status='1'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit="",0,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## checking state name exist or not 
	function checkStateName($statename, $id='') {
		$fields = array();	
		$tables = array($this->state);
		$where= array("state_name='".$statename."'");
		
		if($id != '') {
			$where[] = " state_id!=".$id;	
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	## checking state name exist or not 
	function checkStateNameByName($statename) {
		$fields = array();	
		$tables = array($this->state);
		$where= array("state_name  like '".$statename."%'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	function getStateNameCodeById($stateId)
	{
		$fields=array('iso1_code');	
		$tables=array($this->state);
		$where= array('state_id='.$stateId);
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result['iso1_code'];
	}
	
	function getcntposatlcodeBystateISO($isocode)
	{
		$fields = array('count(id) as postcnt');	
		$tables = array($this->Postalcode);
		$result1 = $this->SelectData($fields,$tables, $where= array("state='".$isocode."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result['postcnt'];
	}
	
	## List all postal code
	function getAllPostalcode($searchindex='',$isocode='', $col1='',$col2='', $limit='',$offset='') {
		$fields = array('p.*');	
		$tables = array($this->state." AS s LEFT JOIN ".$this->Postalcode." AS p ON s.iso1_code = p.state");
		$where= array();
		if($isocode!='')
		{
			$where[] = "state = '".$isocode."'";
		}		
		if($searchindex != '' && $searchindex!='Suburb') {
			$where[] = "iso1_code LIKE '%".trim($searchindex)."%'";
		}
		if($col1 =='0')
		{
			$order = array("s.iso1_code ASC");
		}
		if($col2 =='0')
		{
			$order = array("postcode ASC");
		}		
		if($col1 =='1')
		{
			$order = array("s.iso1_code DESC");
		}
		if($col2 =='1')
		{
			$order = array("postcode DESC");
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get postal details by id
	function getPostalDetailsById($postid) {
		$fields = array();	
		$tables = array($this->Postalcode);
		$result1 = $this->SelectData($fields,$tables, $where= array("id=".$postid), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}
	
	## Get postal details by postalcode
	function getPostalCodeDetailsByPostalId($postcode) {
		$fields = array();	
		$tables = array($this->Postalcode);
		$result1 = $this->SelectData($fields,$tables, $where= array("postcode='".$postcode."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}
	
	## Edit postalcode by id
	function editPostalCodeById($array, $postid){
		$this->UpdateData($this->Postalcode,$array,"id",$postid,0);
	}
	
	## List all suburb name
	function getAllSuburb($isocode='') {
		$fields = array('');	
		$tables = array($this->Postalcode);
		if($isocode!='')
			$where= array("state ='".$isocode."'");
			
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## checking suburb name exist or not 
	function checkSuburbNameAndPostCode($suburbname, $postcode='') {
		$fields = array();	
		$tables = array($this->Postalcode);
		$where= array("suburb='".$suburbname."'");
		
		if($postcode != '') {
			$where[] = " postcode ='".$postcode."'";	
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	## Get postalcode details by postalcode
	function getPostalDetailsByPostalCode($postalcode) {
		$fields = array();	
		$tables = array($this->Postalcode);
		$result1 = $this->SelectData($fields,$tables, $where= array("postcode ='".$postalcode."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}
	#FOR Check the duplicate entry
	function getPostalcodeExits($postalcode)
	{
		$fields=array();
		$tables=array($this->Postalcode);
		$where=array("postcode 	='".$postalcode."'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	function getPostalcodeDetailByIso($isocode)
	{
		$fields=array();
		$tables=array($this->Postalcode);
		$where=array("state ='".$isocode."'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(""), $group=array("postcode"),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	## Get state details by iso_code
	function getStateDetailsByISOCODE($iso_code) {
		$fields = array();	
		$tables = array($this->state);
		$result1 = $this->SelectData($fields,$tables, $where= array("iso1_code='".$iso_code."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}

}
?>